"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/10/31 10:26
@Description :   自定义异常

"""


#  自定义异常  继承Exception
class AgeError(Exception):
    pass


class Student:
    def __init__(self, age):
        if 1 <= age <= 50:
            self.__age = age
        else:
            #  抛出错误信息    raise  主动抛出错误信息  AgeError
            raise AgeError()

    def set_age(self, age):
        if 1 <= age <= 50:
            self.__age = age
        else:
            #  抛出错误信息    raise  主动抛出错误信息  AgeError
            raise AgeError()

    def __str__(self):
        return f"age = {self.__age}"


try:
    student = Student(20)
    print(student)
    student.set_age(60)
except AgeError:
    print('请输入合理的年龄')
